﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core
{
    public static partial class user
    {
        public static dynamic getUserStatistics(string username, string myusername)
        {
            var sql = new Sql.SqlDataContext();
            var u = sql.User.FirstOrDefault(row => row.username.ToLower() == username.ToLower());
            if (u == null) return new
            {
                success = false,
                msg = "用户不存在"
            };
            var us = sql.UserStatistics.FirstOrDefault(row => row.username == u.username);
            if (us == null) us = new Sql.UserStatistics();

            var problemAccept = from so in sql.Solve
                                where so.username == u.username && so.accept > 0
                                from s in sql.Status
                                from p in sql.Problem
                                from l in sql.Language
                                where s.rid == so.bestRid && p.pid == s.pid && l.code == s.lang
                                orderby s.pid
                                select new
                                {
                                    s.rid,
                                    so.pid,
                                    problemTitle = p.title,
                                    so.accept,
                                    so.submit,
                                    s.exeTime,
                                    s.exeMem,
                                    codeLen = s.code.Length,
                                    allowCode = u.username == myusername,
                                    lang = l.title,
                                    langCode = s.lang
                                };
            var problemFail = from so in sql.Solve
                              where so.username == u.username && so.accept == 0 && so.submit > 0
                              from p in sql.Problem
                              where p.pid == so.pid
                              orderby so.pid
                              select new
                              {
                                  so.pid,
                                  problemTitle = p.title,
                                  so.submit
                              };
            
            var psub = sql.Solve.Count(row => row.username == u.username && row.submit > 0);
            var pac = problemAccept.Count();

            var statistics = new List<statistics>();
            statistics.Add(new statistics(20, "Total Submissions", us.submit));
            statistics.Add(new statistics(3, "Accepted", us.accept));
            statistics.Add(new statistics(21, "Problems Submitted", psub));
            statistics.Add(new statistics(22, "Problems Solved", pac));
            statistics.Add(new statistics(4, "Presentation Error", us.pe));
            statistics.Add(new statistics(5, "Time Limit Exceeded", us.tle));
            statistics.Add(new statistics(6, "Memory Limit Exceeded", us.mle));
            statistics.Add(new statistics(7, "Output Limit Exceeded", us.ole));
            statistics.Add(new statistics(8, "Wrong Answer", us.wa));
            statistics.Add(new statistics(9, "Runtime Error", us.re));
            statistics.Add(new statistics(10, "Compile Error", us.ce));
            statistics.Add(new statistics(11, "System Error", us.se));

            return new
            {
                success = true,
                u.username,
                statistics,
                problemAccept,
                problemFail
            };
        }
    }
}
